/*
 * @Author: gxx
 * @QQ:1278255275
 * @Date: 2021-04-07 17:21:48
 * @Last Modified by: gxx
 * @Last Modified time: 2021-04-07 17:21:48
 * @Description: 
*/

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;

int maxProfit(vector<int>& prices){
    if(prices.empty()) return 0;

    int s1 = -prices[0];
    int s2 = INT_MIN;
    int s3 = INT_MIN;
    int s4 = INT_MIN;

    for(int i = 1; i < prices.size(); i++){
        s1 = max(s1,-prices[i]);
        s2 = max(s2,s1+prices[i]);
        s3 = max(s3,s2-prices[i]);
        s4 = max(s4,s3+prices[i]);
    }
    return max(0,s4);
}